<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">患者名字</label>
        <el-input
          v-model="query.nickName"
          clearable
          placeholder="患者名字"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!--          <el-form-item label="患者ID" prop="patientId">-->
          <!--            <el-input v-model="form.patientId" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <el-form-item label="选择患者" prop="patientId">
            <el-select
              v-model="form.patientId"
              filterable
              style="width: 370px"
              multiple
              placeholder="请选择"
              default-first-option
              @remove-tag="deletepatient"
              @change="changePatient"
            >
              <el-option
                v-for="item in patientList"
                :key="item.patientId"
                filterable
                :label="item.nickName"
                :value="item.patientId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="患者姓名">
            <el-input v-model="form.nickName" :disabled="crud.status.title == '编辑患者管理'" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="医疗历史">
            <el-input v-model="form.medicalHistory" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="过敏信息">
            <el-input v-model="form.allergies" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="紧急联系人信息">
            <el-input v-model="form.emergencyContact" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="patientId" label="患者ID" />
        <el-table-column prop="nickName" label="患者名字" />
        <el-table-column prop="medicalHistory" label="医疗历史" />
        <el-table-column prop="allergies" label="过敏信息" />
        <el-table-column prop="emergencyContact" label="紧急联系人信息" />
        <el-table-column v-if="checkPer(['admin','sysPatient:edit','sysPatient:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSysPatient, { getAllPatient } from '@/api/system/sysPatient'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'

let userPatient = []
const defaultForm = { patientId: null, medicalHistory: null, allergies: null, emergencyContact: null, createTime: null, updateTime: null }
export default {
  name: 'SysPatient',
  // eslint-disable-next-line vue/no-unused-components
  components: { DateRangePicker, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '患者管理', url: 'api/sysPatient', idField: 'patientId', sort: 'patientId,desc', crudMethod: { ...crudSysPatient }})
  },
  data() {
    return {
      patientList: [],
      patients: [],
      permission: {
        add: ['admin', 'sysPatient:add'],
        edit: ['admin', 'sysPatient:edit'],
        del: ['admin', 'sysPatient:del']
      },
      rules: {
        patientId: [
          { required: true, message: '患者ID不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增前将多选的值设置为空
    [CRUD.HOOK.beforeToAdd]() {
      this.patientList = []
      this.patients = []
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      // form = null
      console.log(form)
      const tempData = {
        nickName: form.nickName,
        patientId: form.patientId
      }
      console.log(crud.status.title)
      this.patientList.push(tempData)
      this.getPatients()
    },
    // 获取弹窗里面的医生数据
    getPatients() {
      getAllPatient().then(res => {
        this.doctors = res
      }).catch(() => {
      })
    },
    deletepatient(value) {
      userPatient.forEach(function(data, index) {
        if (data.id === value) {
          userPatient.splice(index, value)
        }
      })
    },
    changePatient(value) {
      userPatient = []
      value.forEach(function(data, index) {
        console.log(data)
        const patient = { id: data }
        defaultForm.patientId = data
        userPatient.push(patient)
      })
    }
  }
}
</script>

<style scoped>

</style>
